﻿using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using SpotOffer.Constants;
using SpotOffer.DataModels;
using SpotOffer.Models;
using SpotOffer.Services;
using SpotOffer.Services.HttpClient;
using Xamarin.Forms;

[assembly: Dependency(typeof(DealListService))]
namespace SpotOffer.Services
{
    public class DealListService : IDealListService
    {
        public CityDeals Deals { get; set; }

        public async Task<CityDeals> GetCity(int id)
        {
            HttpResponseMessage res =
           await RestClient.GetClient().GetAsync(ServiceConstants.DealEndpoint + id);
            res.EnsureSuccessStatusCode();
           // StreamReader readStream = new StreamReader(await res.Content.ReadAsByteArrayAsync(), Encoding.UTF8);
         // = readStream.ReadToEnd();

            var arr = await res.Content.ReadAsByteArrayAsync();
            string json = Encoding.UTF8.GetString(arr, 0, arr.Length);
            json=System.Net.WebUtility.HtmlDecode(json);

            Deals = ((CityDeals)JsonConvert.DeserializeObject(json, typeof(CityDeals)));
            foreach (var deal in Deals.deals)
            {
                deal.imagecellprice = deal.price + " " + deal.price_orig + " " + deal.discount;
            }
            return Deals;
        }


    }
}
